package com.xiaolei.library.Net;
import java.util.*;

import java.util.Map;

/**
 * 将Url分解成 参数的形式，toString则再次重组
 */
public class UrlCompiler
{
    public static UrlCompiler parse(String url)
    {
        UrlCompiler instance = new UrlCompiler();
        String[] urlAndParamsStr = url.split("\\?");
        if (urlAndParamsStr.length >= 1)
        {
            String host = urlAndParamsStr[0];
            instance.url = host;
        }
        if (urlAndParamsStr.length >= 2)
        {
            String params = urlAndParamsStr[1];
            String[] keyValues = params.split("&");
            for (int i = 0; i < keyValues.length; i++)
            {
                String keyValue = keyValues[i];
                String[] keyAndValue = keyValue.split("=");
                if (keyAndValue.length == 2)
                {
                    String key = keyAndValue[0];
                    String value = keyAndValue[1];

                    LinkedList<String> values;
                    if (instance.params.containsKey(key))
                    {
                        values = instance.params.get(key);
                    } else
                    {
                        values = new LinkedList<String>();
                        instance.params.put(key, values);
                    }
                    values.add(value);
                }
            }
        }

        return instance;
    }

    private String url = "";
    private Map<String, LinkedList<String>> params = new LinkedHashMap<String, LinkedList<String>>();

    public String getUrl()
    {
        return url;
    }

    public void setUrl(String url)
    {
        this.url = url;
    }

    public Map<String, LinkedList<String>> getParams()
    {
        return params;
    }

    public synchronized void addParam(String key, String value)
    {
        LinkedList<String> values;
        if (params.containsKey(key))
        {
            values = this.params.get(key);
        } else
        {
            values = new LinkedList<String>();
            this.params.put(key, values);
        }
        values.add(value);
    }

    public void deleteParam(String key)
    {
        this.params.remove(key);
    }

    @Override
    public String toString()
    {
        StringBuilder builder = new StringBuilder();
        builder.append(url);
        if (!params.isEmpty())
        {
            builder.append("?");
            for (Map.Entry<String, LinkedList<String>> entry : params.entrySet())
            {
                String name = entry.getKey();
                LinkedList<String> values = entry.getValue();
                for (int i = 0; i < values.size(); i++)
                {
                    String value = values.get(i);
                    builder.append(name).append("=").append(value).append("&");
                }
            }
            if (builder.toString().endsWith("&"))
            {
                builder.delete(builder.length() - 1, builder.length());
            }
        }
        return builder.toString();
    }
}
